package ch.subgate.pongwars;

import ch.subgate.pongwars.GameView.GameThread;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class GameActivity extends Activity {
	
	private GameView mGameView;
	
	private GameThread mGameThread;

    private BluetoothAdapter mBluetoothAdapter = null;

    private BluetoothSocket mBluetoothSocket = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gameactivity_layout);

        // Check the calling intent for settings
        Intent i = this.getIntent();
        int mode = i.getIntExtra("mode", Constants.MODE_PVE);
        float size = i.getFloatExtra("size", Constants.PADDLE_EASY);
        boolean isServer = i.getBooleanExtra("role", true);
        String addr = i.getStringExtra("mac");

        // Get the instances of GameView and GameThread and get started
        mGameView = (GameView)findViewById(R.id.gameview);
        mGameThread = mGameView.getThread();

        if (mode == Constants.MODE_PVPN) {
            Toast.makeText(getApplicationContext(), "wait for connection " + addr, Toast.LENGTH_LONG).show();
            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (isServer) {
                // make device discoverable
                startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE));
                // open server socket
                Thread t = new AcceptThread(this);
                t.start();
            } else {
                // open client socket
                Thread t = new ConnectThread(addr, this);
                t.start();
            } // if
        } // if

        mGameThread.setGameMode(mode, size, isServer);
        if (mode != Constants.MODE_PVPN) {
            mGameThread.setRunning(true);
            mGameThread.start();
        } // if
	} // onCreate

    public void manageConnectedSocket(BluetoothSocket socket) {
        if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); }
        mBluetoothSocket = socket;
        mGameThread.setSocket(mBluetoothSocket);
        mGameThread.setRunning(true);
        mGameThread.start();
    } // manageServerSocket
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            this.getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        } // if
    } // onWindowFocusChanged
	
	@Override
	protected void onDestroy() {
		//Finally stopping the thread
    	mGameThread.setRunning(false);
        try {
            mGameThread.join();
        } catch (InterruptedException e) {
            // do nothing
        }
		super.onDestroy();
	}
	
}
